package cn.cs.controller;

import cn.cs.common.Dto;
import cn.cs.common.DtoUtil;
import cn.cs.common.EmptyUtils;
//import cn.cs.pojo.BaseQuery;
import cn.cs.pojo.Product;
import cn.cs.service.ProductService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Resource
    private ProductService proservice;

    public static String URL="http://47.99.64.150:8022/solr/collection2/";
//    private static HttpSolrClient httpSolrClient = new HttpSolrClient("http://47.99.64.150:8022/solr/collection2/");
//    private BaseQuery<Product> baseQuery = new BaseQuery(URL);

    /****
     * 根据ID查询商品详情接口
     * @param proId 商品id
     * @param
     * @return
     */
    @RequestMapping("/getProInfoById")
    @ResponseBody
    public Dto getProInfoById(@RequestParam String proId, @RequestParam String type, HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            String token = request.getHeader("token");
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto= proservice.getProInfoById(proId,type);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","0002");
        }
        return dto;
    }

    /****
     * 平台合作配件展示接口
     * @param
     * @param
     * @return
     */
    @RequestMapping("/showProduct")
    @ResponseBody
    public Dto showProduct(HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=proservice.showProduct(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","0002");
        }
        return dto;
    }



//    @RequestMapping("/proSearch")
//    @ResponseBody
//    public Dto proSearch(HttpServletRequest request,Integer pageNo, Integer pageSize)throws Exception{
//        String name= request.getParameter("proName");
//        SolrQuery query = new SolrQuery() ;
//        StringBuffer tempQuery = new StringBuffer();
//        String lon = request.getParameter("lon");
//        String lat = request.getParameter("lat");
//        String loc = lat+","+lon;
//        String type = request.getParameter("type");
//
//        //34.608017,113.733434
//        //pt指的是给定的位置
//        //sfield指的是在solr中通过经纬度拼接出来的各个店面的店面经纬度
//        //d指的是范围，比如设置为1，那么是方圆1公里之内
//        tempQuery.append("{!geofilt pt="+loc+" sfield=loc d=10}");
//        if (EmptyUtils.isNotEmpty(name)) {
//            query.setQuery(" product_info:"+name+ "or product_name:"+name );
//        }
//        query.addFilterQuery(tempQuery.toString());
//        //按距离升序排序
//        if("1".equals(type)){
//            query.addSort("geodist(loc,"+loc+")", SolrQuery.ORDER.desc);
//        }
//        if("2".equals(type)){
//            query.addSort("geodist(loc,"+loc+")", SolrQuery.ORDER.asc);
//        }
//        if("3".equals(type)){
//            query.addSort("count", SolrQuery.ORDER.desc);
//        }
//
//        //dist指的是最终计算出来的距离的字段
//        query.setFields("*,dist:geodist(loc,"+loc+")");
//        SolrDocumentList list  = baseQuery.queryString(query);
//        return DtoUtil.returnDataSuccess(list);
//    }



    /****
     * 根据商铺id获取商铺信息（商铺首页）
     * @param
     * @param
     * @return
     */
    @RequestMapping("/getAgentShopById")
    @ResponseBody
    public Dto getAgentShopById(HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            String token = request.getHeader("token");
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=proservice.getAgentShopById(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("获取失败","002");
        }
        return dto;
    }

    /****
     * 商铺产品分类排序
     * @param
     * @param
     * @return
     */
    @RequestMapping("/getShopPro")
    @ResponseBody
    public Dto getShopPro(HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            String token = request.getHeader("token");
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=proservice.getShopPro(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("获取失败","002");
        }
        return dto;
    }

    /**
     * 分页查询经销商产品列表
     * @param pageNumber
     * @param pageSize
     * @param productName
     * @param typeId
     * @param name
     * @param agentId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/getProductByPage")
    public Dto getProductByPage(Integer pageNumber, Integer pageSize, String productName, String typeId, String name, String agentId) throws Exception {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("productName", productName);
        param.put("typeId", typeId);
        param.put("name", name);
        param.put("agentId", agentId);
        return DtoUtil.returnSuccess("获取成功", proservice.queryProductPageByMap(param, pageNumber, pageSize));
    }

}
